<?php
namespace app\api\controller;

use app\api\controller\Base;

/**
 * 公共信息
 */
class Paypwd extends Base
{
    /**
     * 设置用户支付密码
     */
    public function setpaypwd()
    {
        $user_id = $this->user_id;
        $user = db('user')->where(['user_id' => $user_id])->find();
        // 验证账号
        if (isset($user['paypwd']) && $user['paypwd'] != '') {
            return json(['code' => 0, 'msg' => '已设置支付密码']);
        }
        if ($user['user_status'] == 1) {
            return json(['code' => 0, 'msg' => '该账号已禁用']);
        }
        // 验证密码
        $paypwd = input('paypwd');
        if (!isset($paypwd)) {
            return model('Res')->noParam();
        }
        $leng = strlen($paypwd);
        if ($leng != 6) {
            return json(['code' => 0, 'msg' => '密码长度为6位']);
        }
        if (!preg_match('/^[A-Za-z0-9]+$/i', $paypwd)) {
            return json(['code' => 0, 'msg' => '密码必须为字母与数字组合使用']);
        }
        $paypwd = md5($paypwd);
        $list = db('user')->where(['user_id' => $user_id])->update(['paypwd' => $paypwd]);
        if ($list) {
            return json(['code' => 1, 'msg' => '设置成功']);
        } else {
            return json(['code' => 1, 'msg' => '系统繁忙']);
        }

    }
    // 修改密码
    public function modifypassword()
    {
        $user_id = $this->user_id;
        $paypwd = input('paypwd');
        $old_pwd = input('old_pwd');
        //验证参数
        if (!isset($paypwd)) {
            return model('Res')->noParam();
        }
        if (!isset($old_pwd)) {
            return model('Res')->noParam();
        }
        $user = db('user')->where(['user_id' => $user_id])->find();
        // 验证账号
        if (!isset($user['paypwd']) && $user['paypwd'] == '') {
            return json(['code' => 0, 'msg' => '还未设置支付密码']);
        }
        if ($user['user_status'] == 1) {
            return json(['code' => 0, 'msg' => '该账号已禁用']);
        }
        // 验证原密码
        $old_pwd = md5($old_pwd);
        if ($user['paypwd'] != $old_pwd) {
            return json(['code' => 0, 'msg' => '原密码错误,修改失败']);
        }
        // 验证密码
        $paypwd = input('paypwd');
        if (!isset($paypwd)) {
            return model('Res')->noParam();
        }
        $leng = strlen($paypwd);
        if ($leng != 6) {
            return json(['code' => 0, 'msg' => '密码长度为6位']);
        }
        if (!preg_match('/^[A-Za-z0-9]+$/i', $paypwd)) {
            return json(['code' => 0, 'msg' => '密码必须为字母与数字组合使用']);
        }
        $paypwd = md5($paypwd);
        if ($paypwd == $user['paypwd']) {
            return json(['code' => 0, ',msg' => '新旧支付密码不能一致']);
        }
        $list = db('user')->where(['user_id' => $user_id])->update(['paypwd' => $paypwd]);
        if ($list) {
            return json(['code' => 1, 'msg' => '更改成功']);
        } else {
            return json(['code' => 1, 'msg' => '系统繁忙']);
        }
    }

    public function info()
    {
        $list = model('HotelModel')->getHotelInfo();
        $com = db('comment')->where(['status' => 1])->select();
        if ($com) {
            $list['comleng'] = count($com);
        } else {
            $list['comleng'] = 0;
        }

        return model('Res')->ctx($list);
    }
    // 验证密码准确性
    public function verify(){
        $paypwd = input('paypwd');
        if(!isset($paypwd)){
            return json(['code'=>0,'msg'=>'请输入密码']);
        }
        $user_id = $this->user_id;
        $user = db('user')->where(['user_id'=>$user_id])->find();
        $paypwd = md5($paypwd);
        if($user['paypwd']==$paypwd){
            return ['code'=>1,'msg'=>'密码正确'];
        }else{
            return ['code'=>0,'msg'=>'密码错误'];
        }
    }
    // 检查用户是否需要设置支付密码
    public function bool(){
        $user_id = $this->user_id;
        $user = db('user')->where(['user_id'=>$user_id])->find();
        // 判断用户的支付密码与余额是否同时存在
        if(isset($user['money']) && $user['money'] != 0 && isset($user['paypwd']) && $user['paypwd'] != '' ){
            return json(['code'=>1,'data'=>1]);
        }
        // 判断用户的次卡次数与支付密码是否同时存在
        if(isset($user['second_num']) && $user['second_num'] != 0 && isset($user['paypwd']) && $user['paypwd'] != '' ){
            return json(['code'=>1,'data'=>1]);
        }
        // 判断用户是否有支付密码
        if(isset($user['paypwd']) && $user['paypwd'] != ''){
            return json(['code'=>1,'data'=>1]);
        }
        // 需要设置支付密码的情况
        return json(['code'=>1,'data'=>0]);


    }

}
